<template>
  <div class="app-container">
    <!-- 搜索筛选操作栏 -->
    <div class="filter-container clearfix">
      <el-input
        v-model="listQuery.code"
        placeholder="请输入账号"
        style="width: 200px"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-input
        v-model="listQuery.name"
        placeholder="请输入店铺名称"
        style="width: 200px; margin-left: 10px"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-select
        v-model="listQuery.status"
        placeholder="请选择状态"
        clearable
        style="width: 150px; margin-left: 10px"
        class="filter-item"
      >
        <el-option label="待审核" :value="0" />
        <el-option label="已同意" :value="1" />
        <el-option label="已拒绝" :value="2" />
      </el-select>
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        style="margin-left: 10px"
        @click="handleFilter"
      >
        搜索
      </el-button>
      <el-button
        v-waves
        class="filter-item"
        icon="iconfont qt-caozuo-xunhuan1"
        @click="clearFilter"
      >
        清除条件
      </el-button>
    </div>

    <!-- 表格 -->
    <el-table
      :key="tableKey"
      ref="table"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column label="账号" prop="user.user" align="center" />
      <el-table-column label="店铺名称" align="center">
        <template slot-scope="{ row }">
          {{
            (row.shopping && row.shopping.shopping_name) ||
              (row.shopping && row.shopping.name)
          }}
        </template>
      </el-table-column>
      <el-table-column label="金额" prop="money" align="center" />
      <el-table-column label="操作后余额" prop="balance_money" align="center" />
      <el-table-column label="提现方式" width="170px">
        <template slot-scope="{ row }">
          <div>
            {{
              row.pay_type == 0
                ? "支付宝"
                : row.pay_type == 1
                  ? "微信"
                  : row.pay_type == 2
                    ? "银行卡"
                    : row.pay_type == 3
                      ? "微信公众号"
                      : ""
            }}
          </div>
          <div v-if="row.pay_type == 2">
            {{ row.user && row.user.bank_card_name }}({{
              row.user && row.user.branch_name
            }})
          </div>
          <div v-if="row.pay_type == 2">
            {{ row.user && row.user.id_card }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="申请时间" prop="created_at" />
      <el-table-column label="状态" align="center">
        <template slot-scope="{ row }">
          {{
            row.status == 0
              ? "待审核"
              : row.status == 1
                ? "已拒绝"
                : row.status == 2
                  ? "已同意"
                  : ""
          }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="{ row }">
          <el-button
            v-if="row.status == 0"
            size="mini"
            plain
            type="success"
            @click="handleAgree(row.id)"
          >
            同意已转款
          </el-button>
          <el-button
            v-if="row.status == 0"
            size="mini"
            plain
            type="danger"
            @click="handleRefuse(row.id)"
          >
            拒绝
          </el-button>
          <span v-if="row.status != 0">-</span>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div v-if="total > 0" style="text-align: right">
      <pagination
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.per_page"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script>
import { getBondcashoutList, updateBondcashout } from '@/api/common'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'FinanceBondCashOut',
  components: {
    Pagination
  },
  directives: {
    waves
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      form: {},
      listQuery: {
        page: 1,
        per_page: 20,
        name: undefined,
        code: undefined,
        status: undefined
      },
      rules: {
        order: [
          {
            required: true,
            message: '请输入排序',
            trigger: 'blur'
          }
        ],
        name: [
          {
            required: true,
            message: '请输入分类名称',
            trigger: 'blur'
          }
        ],
        parent_id: [
          {
            required: true,
            message: '请选择上级分类',
            trigger: 'change'
          }
        ],
        image_url: [
          {
            required: true,
            message: '请上传图片',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    clearFilter() {
      this.listQuery['name'] = undefined
      this.listQuery['code'] = undefined
      this.listQuery['status'] = undefined
      this.handleFilter()
    },
    getList() {
      this.listLoading = true
      getBondcashoutList(this.listQuery)
        .then((response) => {
          const { data, total } = response.data
          this.list = data
          this.total = total
        })
        .finally(() => {
          this.listLoading = false
        })
    },
    handleAgree(id) {
      this.$confirm('是否确认同意并转款', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          updateBondcashout(id, { status: '2' }).then(() => {
            this.getList()
            this.$notify({
              title: '提示',
              message: '操作成功',
              type: 'success',
              duration: 2000
            })
          })
        })
        .catch(() => {})
    },
    // 拒绝
    handleRefuse(id) {
      const that = this
      this.$confirm('是否确认拒绝', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          updateBondcashout(id, {
            status: '1'
          }).then(() => {
            that.getList()
            that.$notify({
              title: '提示',
              message: '操作成功',
              type: 'success',
              duration: 2000
            })
          })
        })
        .catch(() => {})
    },
    // 查看
    handleLook(row) {
      this.$router.push('/member/detail/' + row.id)
    }
  }
}
</script>
